package com.scap.dfs.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.scap.dfs.dao.AbstractSessiontFactoryDao;
import com.scap.dfs.dao.AccountDao;
import com.scap.dfs.entity.Account;

@Service
@Repository("voAccountDao")
public class AccountDaoImpl extends AbstractSessiontFactoryDao implements
		AccountDao {

	private static final Logger logger = LoggerFactory.getLogger(AccountDaoImpl.class);
		
	@SuppressWarnings("unchecked")
	@Override
	public List<Object> findByAll() {
		Criteria criteria = getSession().createCriteria(Account.class);
		List<Object> voAccountList = (List<Object>) criteria.list();
		shutdownSession();
		return voAccountList;
	}

	@Override
	public boolean insert(Account voAccount) {
		boolean result = false;
		try {
			persist(voAccount);
			result = true;
		} catch (Exception e) {
			logger.error(e.getMessage().toString());
			result = false;
		}
		return result;
	}

	@Override
	public boolean update(Account voAccount) {

		boolean result = false;

		try {

			Query query = getSession().createQuery(
					"update Account set vaAccountDescription = :vaAccountDescription "
							+ " where vaAccountCode = :vaAccountCode");

			query.setString("vaAccountDescription",
					voAccount.getVaAccountDescription());
			query.setString("vaAccountCode", voAccount.getVaAccountCode());
			query.executeUpdate();
			result = true;
		} catch (Exception e) {
			logger.error(e.getMessage().toString());
			result = false;
		}
		return result;
	}

	@Override
	public boolean delete(Account voAccount) {
		boolean result = false;
		try {
			delete(voAccount);
			result = true;
		} catch (Exception e) {
			logger.error(e.getMessage().toString());
			result = false;
		}
		return result;
	}

}
